package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.config.MiniserveConfig;
import com.uex.dev.manage.admin.dao.ProjectDao;
import com.uex.dev.manage.admin.po.ProjectPO;
import com.uex.dev.manage.admin.service.IProjectService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.project.ProjectInfo;
import com.uex.dev.manage.admin.vo.project.ProjectListReq;
import com.uex.dev.manage.admin.vo.project.ProjectListResp;
import com.uex.dev.manage.common.base.PageResult;
import com.uex.dev.manage.common.utils.RandomUtils;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 设备所属项目信息 仅内部用户查看 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-22
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectPO> implements IProjectService {

    @Resource
    private MiniserveConfig miniserveConfig;

    @Override
    public ProjectInfo getPrjInfo(Integer prjId) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(prjId), "项目Id不能为空");
        ProjectPO project = this.getById(prjId);
        Preconditions.checkArgument(ObjectUtils.isNotNull(project), "项目Id不存在");
        ProjectInfo projectInfoResp = new ProjectInfo();
        BeanUtils.copyProperties(project, projectInfoResp);
        projectInfoResp.setPrjWorkModeDesc(Objects.nonNull(project.getPrjWorkMode()) ? project.getPrjWorkMode().getDesc() : "-");
        LogUtil.log(0, "获取项目基本信息");
        return projectInfoResp;
    }

    @Override
    public void updatePrjInfo(ProjectPO projectUpdateInfoReq) {
        if (ObjectUtils.isNull(projectUpdateInfoReq.getPrjId()) || projectUpdateInfoReq.getPrjId() == 0) {
            // 添加项目
            Preconditions.checkArgument(StringUtils.isNotBlank(projectUpdateInfoReq.getPrjClientCompany()), "销售客户不能为空");
            projectUpdateInfoReq.setPrjEndService(RandomUtils.randomNum(6));
            this.save(projectUpdateInfoReq);
            miniserveMkdirDirectory(projectUpdateInfoReq.getPrjId().toString());
            LogUtil.log(0, "新增项目");
        }else{
            // 更新项目
            Preconditions.checkArgument(this.updateById(projectUpdateInfoReq), "项目Id不存在");
            LogUtil.log(0, "更新项目基本信息");
        }
    }

    @Override
    public PageResult<ProjectListResp> getProjectList(ProjectListReq projectListReq) {
        Page<ProjectPO> projectPage = new Page<>(projectListReq.getPage(), projectListReq.getRecord());
        Page<ProjectPO> result = this.page(projectPage, new LambdaQueryWrapper<ProjectPO>()
                .like(StringUtils.isNotBlank(projectListReq.getPrjSaler()), ProjectPO::getPrjSaler, projectListReq.getPrjSaler())
                .like(StringUtils.isNotBlank(projectListReq.getPrjClientCompany()), ProjectPO::getPrjClientCompany, projectListReq.getPrjClientCompany())
                .orderByDesc(ProjectPO::getPrjId));
        return PageResult.build(result).convert(ProjectListResp::fromPO);
    }

    private void miniserveMkdirDirectory(String projectId) {
        String fileDir = miniserveConfig.getPath() + miniserveConfig.getFile().replace("{project}", projectId);
        System.out.println(fileDir);
        try {
            FileUtils.forceMkdir(new File(fileDir));
        } catch (Exception e) {
            log.error("[AdminDeviceService] mkdir error", e);
        }
    }
}
